import { createRouter, createWebHistory } from 'vue-router'

export const routes = [
  {
    path: '/',
    name: 'home',
    component: () => import('@/views/Home/Home.vue'),
    children: [
      {
        path: 'newspool',
        name: 'newspool',
        menu: '新闻池',
        component: () => import('@/views/Newspool/Newspool.vue'),

      },
      {
        path: 'newspublish',
        name: 'newspublish'
        , menu: '新闻发布',
        component: () => import('@/views/NewsPublish/NewsPublish.vue'),

      },
      {
        path: 'newspublished'
        , menu: '已发布新闻',
        name: 'newspublished',
        component: () => import('@/views/NewsPublished/NewsPublished.vue'),

      },
      {
        path: 'system'
        , menu: '系统管理',
        name: 'system',
        component: () => import('@/views/SystemManagement/SystemManagement.vue')
      },
      {
        path: 'log'
        , menu: '日志管理',
        name: 'log',
        component: () => import('@/views/LogManagement/LogManagement.vue')
      },
      {
        path: 'note'
        , menu: '通知管理',
        name: 'note',
        component: () => import('@/views/NoteManagement/NoteManagement.vue')
      },
      {
        path: 'inter'
        , menu: '国际化管理',
        name: 'inter',
        component: () => import('@/views/InterManagement/InterManagement.vue')
      },
      {
        path: 'publishSetting',
        name: 'publishSetting',
        menu: '发布设置',
        component: () => import('@/views/NewsPublish/PublishSetting.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login/Login.vue')
  },
  {
    path: '/preview',
    name: 'preview',
    component: () => import('@/views/Preview/Preview.vue'),
    children: [
      {
        path: 'lookList',
        name: 'lookList',
        component: () => import('@/views/Preview/LookList.vue')
      },
      {
        path: 'lookDetail',
        name: 'lookDetail',
        component: () => import('@/views/Preview/LookDetail.vue')
      }
    ]
  },
]
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
})

router.beforeEach((to, from) => {
  const authenticated: () => boolean = () => {
    const loginInfo = localStorage.getItem('login')
    return !!loginInfo
  }
  if (to.name !== 'login' && !authenticated()) {
    return {
      name: 'login'
    }
  }
  if (to.name === 'login' && authenticated()) {
    return {
      name: 'newspool'
    }
  }
  return true
})

export default router
